package org.codingq92.struct;

/**
 * 二分查找<br/>
 *
 * @author: CodingQ92
 * @date: 2025/6/9 21:46
 */
public class HalfLook {
    public static void main(String[] args) {
        int[] array = new int[]{1, 3, 5, 7, 9};
        System.out.println(halfLook(array, 7));
        System.out.println(halfLook(array, 2));
    }

    private static int halfLook(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }
}
